<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,200分)- 垃圾短信识别（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>大众对垃圾短信深恶痛绝&#xff0c;希望能对垃圾短信发送者进行识别&#xff0c;为此&#xff0c;很多软件增加了垃圾短信的识别机制。<br /> 经分析&#xff0c;发现正常用户的短信通常具备交互性&#xff0c;而垃圾短信往往都是大量单向的短信&#xff0c;按照如下规则进行垃圾短信识别&#xff1a;</p> 
<p>本题中&#xff0c;发送者A符合以下条件之一的&#xff0c;则认为A是垃圾短信发送者&#xff1a;</p> 
<ul><li>A发送短信的接收者中&#xff0c;没有发过短信给A的人数L &gt; 5&#xff1b;</li><li>A发送的短信数 – A接收的短信数M &gt; 10&#xff1b;</li><li>如果存在X&#xff0c;A发送给X的短信数 – A接收到X的短信数N &gt; 5&#xff1b;</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行是条目数&#xff0c;接下来几行是具体的条目&#xff0c;每个条目&#xff0c;是一对ID&#xff0c;第一个数字是发送者ID&#xff0c;后面的数字是接收者ID&#xff0c;中间空格隔开&#xff0c;所有的ID都为无符号整型&#xff0c;ID最大值为100&#xff1b;</p> 
<p>同一个条目中&#xff0c;两个ID不会相同&#xff08;即不会自己给自己发消息&#xff09;</p> 
<p>最后一行为指定的ID</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出该ID是否为垃圾短信发送者&#xff0c;并且按序列输出 L M 的值&#xff08;由于 N 值不唯一&#xff0c;不需要输出&#xff09;&#xff1b;<br /> 输出均为字符串。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">15<br /> 1 2<br /> 1 3<br /> 1 4<br /> 1 5<br /> 1 6<br /> 1 7<br /> 1 8<br /> 1 9<br /> 1 10<br /> 1 11<br /> 1 12<br /> 1 13<br /> 1 14<br /> 14 1<br /> 1 15<br /> 1</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">true 13 13</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">true 表示1是垃圾短信发送者&#xff0c;两个数字&#xff0c;代表发送者1对应的L和M值。<br /> true 13 13中间以<strong>一个空格</strong>分割。注意true是字符串输出。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">15<br /> 1 2<br /> 1 3<br /> 1 4<br /> 1 5<br /> 1 6<br /> 1 7<br /> 1 8<br /> 1 9<br /> 1 10<br /> 1 11<br /> 1 12<br /> 1 13<br /> 1 14<br /> 14 1<br /> 1 15<br /> 2</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">false 0 -1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>感觉是纯逻辑题&#xff0c;解析请看代码注释。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    n &#61; lines[0] - 0;
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 2) {
    lines.shift();
    const id &#61; lines.pop();
    const arr &#61; lines.map((line) &#61;&gt; line.split(&#34; &#34;));

    console.log(getResult(id, arr));
    lines.length &#61; 0;
  }
});

/**
 *
 * &#64;param {*} tid 指定的ID
 * &#64;param {*} arr [[发送者id,接收者id]]
 * &#64;returns
 */
function getResult(tid, arr) {
  // send记录 tid发送短信给“哪些人”
  const send &#61; [];
  // receive记录  “哪些人”发送短信给tid
  const receive &#61; [];

  // sendCount记录 tid发送了“几次”&#xff08;对象属性值&#xff09;短信给某个人&#xff08;对象属性&#xff09;
  const sendCount &#61; {};
  // receiveCount记录 某人&#xff08;对象属性&#xff09;发送了“几次”&#xff08;对象属性值&#xff09;短信给tid
  const receiveCount &#61; {};

  for (let [sid, rid] of arr) {
    if (sid &#61;&#61; tid) {
      send.push(rid);
      sendCount[rid] ? sendCount[rid]&#43;&#43; : (sendCount[rid] &#61; 1);
    }
    if (rid &#61;&#61; tid) {
      receive.push(sid);
      receiveCount[sid] ? receiveCount[sid]&#43;&#43; : (receiveCount[sid] &#61; 1);
    }
  }

  const sendSet &#61; new Set(send);
  const receiveSet &#61; new Set(receive);
  // connect记录和tid有交互的id
  const connect &#61; [...sendSet].filter((id) &#61;&gt; receiveSet.has(id));

  const l &#61; sendSet.size - connect.length;
  const m &#61; send.length - receive.length;

  let isSpammers &#61; l &gt; 5 || m &gt; 10;

  // 如果已经通过l和m确定了tid是垃圾短信发送者&#xff0c;那就不需要再确认n了&#xff0c;否则还是需要确认n
  if (!isSpammers) {
    // for (let id of connect) {
    //   if ((sendCount[id] ?? 0) - (receiveCount[id] ?? 0) &gt; 5) {
    // 根据网友指正&#xff0c;第三条规则中&#xff0c;X不一定非要和A存在交互&#xff0c;即存在A发送6条短信给X&#xff0c;但是X没有发送过短信给A&#xff0c;此时也符合第三条规则
    // 因此&#xff0c;这里id应该遍历自sendSet&#xff0c;而不是connect
    for (let id of sendSet) {
      if (sendCount[id] - (receiveCount[id] ?? 0) &gt; 5) {
        // 一旦发现x,则可以判断&#xff0c;则确定tid是垃圾短信发送者&#xff0c;可提前结束循环
        isSpammers &#61; true;
        break;
      }
    }
  }

  return &#96;${isSpammers} ${l} ${m}&#96;;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.*;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    int[][] arr &#61; new int[n][2];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      arr[i][0] &#61; sc.nextInt();
      arr[i][1] &#61; sc.nextInt();
    }

    int id &#61; sc.nextInt();

    System.out.println(getResult(id, arr));
  }

  public static String getResult(int tid, int[][] arr) {
    // send记录 tid发送短信给“哪些人”
    LinkedList&lt;Integer&gt; send &#61; new LinkedList&lt;&gt;();
    // receive记录  “哪些人”发送短信给tid
    LinkedList&lt;Integer&gt; receive &#61; new LinkedList&lt;&gt;();

    // sendCount记录 tid发送了“几次”&#xff08;对象属性值&#xff09;短信给某个人&#xff08;对象属性&#xff09;
    HashMap&lt;Integer, Integer&gt; sendCount &#61; new HashMap&lt;&gt;();
    // receiveCount记录 某人&#xff08;对象属性&#xff09;发送了“几次”&#xff08;对象属性值&#xff09;短信给tid
    HashMap&lt;Integer, Integer&gt; receiveCount &#61; new HashMap&lt;&gt;();

    for (int[] ele : arr) {
      int sid &#61; ele[0];
      int rid &#61; ele[1];

      if (sid &#61;&#61; tid) {
        send.addLast(rid);
        sendCount.put(rid, sendCount.getOrDefault(rid, 0) &#43; 1);
      }

      if (rid &#61;&#61; tid) {
        receive.addLast(sid);
        receiveCount.put(sid, receiveCount.getOrDefault(sid, 0) &#43; 1);
      }
    }

    HashSet&lt;Integer&gt; sendSet &#61; new HashSet&lt;&gt;(send);
    HashSet&lt;Integer&gt; receiveSet &#61; new HashSet&lt;&gt;(receive);

    // connect记录和tid有交互的id
    List&lt;Integer&gt; connect &#61;
        sendSet.stream().filter(receiveSet::contains).collect(Collectors.toList());

    int l &#61; sendSet.size() - connect.size();
    int m &#61; send.size() - receive.size();

    boolean isSpammers &#61; l &gt; 5 || m &gt; 10;

    // 如果已经通过l和m确定了tid是垃圾短信发送者&#xff0c;那就不需要再确认n了&#xff0c;否则还是需要确认n
    if (!isSpammers) {
      //      for (Integer id : connect) {
      //        if (sendCount.getOrDefault(id, 0) - receiveCount.getOrDefault(id, 0) &gt; 5) {
      // 根据网友指正&#xff0c;第三条规则中&#xff0c;X不一定非要和A存在交互&#xff0c;即存在A发送6条短信给X&#xff0c;但是X没有发送过短信给A&#xff0c;此时也符合第三条规则
      // 因此&#xff0c;这里id应该遍历自sendSet&#xff0c;而不是connect
      for (Integer id : sendSet) {
        if (sendCount.get(id) - receiveCount.getOrDefault(id, 0) &gt; 5) {
          // 一旦发现x,则可以判断&#xff0c;则确定tid是垃圾短信发送者&#xff0c;可提前结束循环
          isSpammers &#61; true;
          break;
        }
      }
    }

    return isSpammers &#43; &#34; &#34; &#43; l &#43; &#34; &#34; &#43; m;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
arr &#61; [input().split() for i in range(n)]
tid &#61; input()


# 算法入口
def getResult(tid, arr):
    # send记录 tid发送短信给“哪些人”
    send &#61; []
    # receive记录  “哪些人”发送短信给tid
    receive &#61; []

    # sendCount记录 tid发送了“几次”&#xff08;对象属性值&#xff09;短信给某个人&#xff08;对象属性&#xff09;
    sendCount &#61; {}
    # receiveCount记录 某人&#xff08;对象属性&#xff09;发送了“几次”&#xff08;对象属性值&#xff09;短信给tid
    receiveCount &#61; {}

    for sid, rid in arr:
        if sid &#61;&#61; tid:
            send.append(rid)
            if sendCount.get(rid) is None:
                sendCount[rid] &#61; 1
            else:
                sendCount[rid] &#43;&#61; 1

        if rid &#61;&#61; tid:
            receive.append(sid)
            if receiveCount.get(sid) is None:
                receiveCount[sid] &#61; 1
            else:
                receiveCount[sid] &#43;&#61; 1

    sendSet &#61; set(send)
    receiveSet &#61; set(receive)

    # connect记录和tid有交互的id
    connect &#61; list(filter(lambda x: x in receiveSet, list(sendSet)))

    l &#61; len(sendSet) - len(connect)
    m &#61; len(send) - len(receive)

    isSpammers &#61; l &gt; 5 or m &gt; 10

    # 如果已经通过l和m确定了tid是垃圾短信发送者&#xff0c;那就不需要再确认n了&#xff0c;否则还是需要确认n
    if not isSpammers:
        # for id in connect:
        #     if (sendCount.get(id) or 0) - (receiveCount.get(id) or 0) &gt; 5:
        # 根据网友指正&#xff0c;第三条规则中&#xff0c;X不一定非要和A存在交互&#xff0c;即存在A发送6条短信给X&#xff0c;但是X没有发送过短信给A&#xff0c;此时也符合第三条规则
        # 因此&#xff0c;这里id应该遍历自sendSet&#xff0c;而不是connect
        for id in sendSet:
            if sendCount.get(id) - (receiveCount.get(id) or 0) &gt; 5:
                isSpammers &#61; True
                # 一旦发现x,则可以判断&#xff0c;则确定tid是垃圾短信发送者&#xff0c;可提前结束循环
                break

    return f&#34;{str(isSpammers).lower()} {l} {m}&#34;


# 算法调用
print(getResult(tid, arr))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>